#include "commen.h"
#include "ui_commen.h"
#include"listitem.h"
#include "musiclist.h"
Commen::Commen(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Commen)
{
    ui->setupUi(this);
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    connect(ui->playAllBtn,&QPushButton::clicked,this,[=](){
        emit playAll(pageType);
    });
    connect(ui->pageMusicList,&QListWidget::doubleClicked,this,[=](const QModelIndex& index){
        emit playMusicByIndex(this,index.row());
    });
}

void Commen::setCommen(const QString &text, const QString &image)
{
   ui->pageTittle->setText(text);
   ui->musicImageLabel->setPixmap(QPixmap(image));
   ui->musicImageLabel->setScaledContents(true);

}

void Commen::setMusicListType(PageType pageType)
{
    this->pageType = pageType;
}

void Commen::reFresh(MusicList &musicList)
{
    ui->pageMusicList->clear();
    addMusicToMusicPage(musicList);
    for(auto musicId : musicListOfPage)
    {
        auto it = musicList.findMusicById(musicId);
        if(it==musicList.end())
        {
            continue;

        }
        listitem* as=new listitem(ui->pageMusicList);
        as->setMusicName(it->getMusicName());
        as->setAlbumName(it->getAlbumName());
        as->setSinger(it->getSingerName());
        as->setLikeIcon(it->getIsLike());
        QListWidgetItem* ad=new QListWidgetItem(ui->pageMusicList);
        ad->setSizeHint(QSize(ui->pageMusicList->width(),45));
        ui->pageMusicList->setItemWidget(ad,as);
        connect(as,&listitem::setIsLike,this,[=](bool isLike){
            emit updateLikeMusic(isLike,it->getMusicId());
        });
    }
    ui->pageMusicList->repaint();
}

void Commen::setImageLabel(QPixmap pixMap)
{
    ui->musicImageLabel->setPixmap(pixMap);
    ui->musicImageLabel->setScaledContents(true);
}

void Commen::addMusicToPlayer(MusicList &musicList, QMediaPlaylist *playlist)
{
    for(auto e:musicList)
    {
        switch(pageType)
        {
            case LOCAL_PAGE:
                playlist->addMedia(e.getMusicUrl());
                break;
            case LIKE_PAGE:
                if(e.getIsLike())
                {
                    playlist->addMedia(e.getMusicUrl());
                }
                break;
            case HISTORY_PAGE:
                if(e.getIsHistory())
                {
                     playlist->addMedia(e.getMusicUrl());
                }
                break;
            default:
                break;
        }
    }
}

QString Commen::getMusicIdByIndex(int index)
{
    if(index>=musicListOfPage.size())
    {
        qDebug()<<"无此歌曲";
        return "";
    }
    return musicListOfPage[index];
}
Commen::~Commen()
{
    delete ui;
}

void Commen::addMusicToMusicPage(MusicList &musicList)
{
    musicListOfPage.clear();
    for(auto& music:musicList)
    {
        switch(pageType)
        {
          case LOCAL_PAGE:
            musicListOfPage.push_back(music.getMusicId());
            break;
          case LIKE_PAGE:
            if(music.getIsLike())
            {
                musicListOfPage.push_back(music.getMusicId());
            }
            break;
          case HISTORY_PAGE:
            if(music.getIsHistory())
            {
                musicListOfPage.push_back(music.getMusicId());
            }
            break;
           default:
            break;
        }
    }
}
